package com.dabai.opdoubleopen.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import java.util.ArrayList;
import java.util.List;

public class AppUtils {


    /**
     * 获取所有包名
     *
     * @param context
     * @return
     */
    public static ArrayList<String> getAllAppPackages(Context context) {

        ArrayList<String> arrayList = new ArrayList<>();

        PackageManager pm = context.getPackageManager();
        List<PackageInfo> list = pm
                .getInstalledPackages(PackageManager.MATCH_UNINSTALLED_PACKAGES);

        for (PackageInfo packageInfo : list) {

            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
                // 获取到设备上已经安装的应用的名字,即在AndriodMainfest中的app_name。
                String appName = packageInfo.applicationInfo.loadLabel(
                        context.getPackageManager()).toString();
                // 获取到应用所在包的名字,即在AndriodMainfest中的package的值。
                String packageName = packageInfo.packageName;
                arrayList.add(packageName);
            }

        }

        return arrayList;
    }

    public static String getApplicationNameByPackageName(Context context, String packageName) {

        PackageManager pm = context.getPackageManager();
        String name;
        try {
            name = pm.getApplicationLabel(pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA)).toString();
        } catch (PackageManager.NameNotFoundException e) {
            name = "";
        }
        return name;
    }

    public static Drawable getApplicationIconByPackageName(Context context, String packageName) {

        PackageManager pm = context.getPackageManager();
        Drawable icon;
        try {
            icon = pm.getApplicationIcon(pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
        } catch (PackageManager.NameNotFoundException e) {
            icon = null;
        }
        return icon;
    }
    public static String getApplicationTargetSdkByPackageName(Context context, String packageName) {

        PackageManager pm = context.getPackageManager();
        String ver;
        try {
            ver = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA).targetSdkVersion+"";
        } catch (PackageManager.NameNotFoundException e) {
            ver = null;
        }
        return ver;
    }

    public static ApplicationInfo getApplicationInfoByPackageName(Context context, String packageName){
        PackageManager pm = context.getPackageManager();

        ApplicationInfo applicationInfo = null;
        try {
            applicationInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return applicationInfo;
    }


    public static String getAppSourceByPackageName(Context context, String packageName){
        PackageManager pm = context.getPackageManager();

        String path = null;
        try {
            path = pm.getApplicationInfo(packageName, 0).sourceDir;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return path;
    }


}
